#include "cla_dorm.h"
#include<QDebug>
#include<QFile>
#include<QFileDialog>
#include<QMessageBox>
#include<QTextStream>
#include<QDebug>
#include<QTextEdit>
#include<QLineEdit>
#include<QTextCodec>
cla_dorm::cla_dorm()
{

}
void cla_dorm::entDorm(QString fileName,QStringList list){
                                              //打开文件
    QFile file(fileName);
    file.open(QIODevice::ReadOnly|QIODevice::Text);
    QTextStream in(&file);
    in.setCodec("UTF-8");
                                              //查找文件中是否有相同信息
    QString str;
    Node n;
    int sig=0;
    QStringList strlist;
    while (!file.atEnd()) {
       str=file.readLine();
       strlist=str.split(",");
       n.l=strlist[0];
       n.s=strlist[1];
       n.score=strlist[2];
       ve.push_back(n);
    }                                         //存入向量,之后进行查找
    file.close();
    for(auto it=ve.begin();it<ve.end();it++){
        if(it->l==list.at(0)&&it->s==list.at(1)){
            QMessageBox::warning(0,QString("error"),QString("该信息已存在"),QMessageBox::Yes);
            sig=1;
            break;
        }
    }

                                             //无相同信息，写入文件
  if(!sig){
                                             //先判断文件是否为空
     file.open(QIODevice::ReadOnly|QIODevice::Text);
     QString a=file.readLine();
     file.close();
     file.open(QIODevice::Append|QIODevice::Text);
     if(a.isEmpty()){
       in<<list.at(0)<<","<<list.at(1)<<","<<list.at(2);
     }else {
       in<<endl<<list.at(0)<<","<<list.at(1)<<","<<list.at(2);
      }
   }
    file.close();

}//信息录入实现
 bool cla_dorm::inqDorm(QString fileName,QString s){
     QFile file(fileName);
     bool sign=0;
     if(!file.open(QIODevice::ReadOnly|QIODevice::Text)){
         QMessageBox msg;
         msg.setText("打开文件失败");
         msg.exec();                        //打开文件检测
     }else{
         QString str;
         Node n;
         QStringList strlist;
         while (!file.atEnd()) {
            str=file.readLine();
            strlist=str.split(",");
            n.l=strlist.at(0);
            n.s=strlist.at(1);
            n.score=strlist.at(2);
            ve.push_back(n);
         }
         file.close();                     //从文件中读出数据,保存到向量中
         SGNode m;
         for(auto it=ve.begin();it!=ve.end();it++){
             if(it->l==s){
                 m.s=it->s;
                 m.score=it->score.toFloat();
                 v.push_back(m);
                 sign=1;
             }
         }                                //找到与条件相符合的数据

         if(!sign){
             QMessageBox::information(0,QString("提示"),QString("该宿舍楼不存在！"),QMessageBox::Ok);
         }else{
                                         //排序
         qSort(v.begin(),v.end(),[](const SGNode& a,const SGNode& b){
                   return a.score>b.score;
             }
             );
         }
      }
     return sign;
 }
 void cla_dorm::toQlistDorm(QString filePath,QStringList &textlist,int &r_count){
     QFile f(filePath);
     f.open(QIODevice::WriteOnly);
     QTextStream in(&f);
     in.setCodec("UTF-8");
     auto it=v.begin();                      //先把第一行写入
     in<<it->s<<"\t"<<it->score;
     for(it=v.begin()+1;it!=v.end();it++){
         in<<endl<<it->s<<"\t"<<it->score;
     }
     f.close();
                                            //将文件内容保存在Qstringlist
     f.open(QIODevice::ReadOnly|QIODevice::Text);
     while(!in.atEnd()){
         QString line=in.readLine();
         textlist.append(line);
         r_count++;
     }
     f.close();                            //将文件内容读入
 }//信息查询实现

 void cla_dorm::modfDorm(QString fileName,QString s1,QString s2,QString s3){
     QFile file(fileName);
     file.open(QIODevice::ReadOnly);
                                          //从文件中读取
     QTextStream in(&file);
     in.setCodec("UTF-8");
     QString str;
     Node n;
     QStringList strlist;
     while (!file.atEnd()) {
        str=file.readLine();
        strlist=str.split(",");
        n.l=strlist[0];
        n.s=strlist[1];
        n.score=strlist[2];
        ve.push_back(n);
     }                                   //存入向量
     file.close();

     int sig=0;
     for(auto it=ve.begin();it<ve.end();it++){
         if(it->l==s1&&it->s==s2){
             it->score=s3+'\n';
             sig=1;
         }
     }                                  //查找，修改
     if(!sig){
         QMessageBox::information(0,QString("error"),QString("该信息不存在"),QMessageBox::Yes);
     }
     else{
             file.open(QIODevice::WriteOnly|QIODevice::Text);
             for(auto it=ve.begin();it<ve.end();it++){
                 in<<it->l<<","<<it->s<<","<<it->score;
                }
             file.close();
         }                              //保存
 }//信息修改实现

void cla_dorm::change(QVector<SGNode>::iterator a,QVector<SGNode>::iterator b){
    QString s;
    float f;
    s=a->s;f=a->score;
    a->s=b->s;a->score=b->score;
    b->s=s;b->score=f;
}
